package com.shanghaiyimu.yimiyangche.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.shanghaiyimu.yimiyangche.R;

public class KeHuJingLiActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView phone,qq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ke_hu_jing_li);
        initView();
    }

    private void initView() {
        phone = (TextView) findViewById(R.id.call_jingli);
        qq = (TextView) findViewById(R.id.qqtojingli);
        phone.setOnClickListener(this);
        qq.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.call_jingli:
                AlertDialog.Builder  aBuilder = new AlertDialog.Builder(this);
                aBuilder.setTitle("拨号"); //设置标头显示的样子
                aBuilder.setIcon(R.mipmap.ic_launcher); //设置图标
                aBuilder.setMessage("给高晓松拨号？");
                /**
                 * 1.Button显示的内容
                 * 2.监听事件
                 */
                aBuilder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
                    @Override  //点击积极按钮触发
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone.getText().toString()));
                        startActivity(intent);
                    }
                });
                //null没有任何逻辑 只是关闭Dialog
                aBuilder.setNegativeButton("取消", null);
                //最后 不要show();
                aBuilder.show();

                break;

            case R.id.qqtojingli:

                AlertDialog.Builder  aBuilder1 = new AlertDialog.Builder(this);
                aBuilder1.setTitle("QQ"); //设置标头显示的样子
                aBuilder1.setIcon(R.mipmap.ic_launcher); //设置图标
                aBuilder1.setMessage("QQ联系高晓松？");
                /**
                 * 1.Button显示的内容
                 * 2.监听事件
                 */
                aBuilder1.setPositiveButton("是的", new DialogInterface.OnClickListener() {
                    @Override  //点击积极按钮触发
                    public void onClick(DialogInterface dialog, int which) {
                        if (checkApkExist(KeHuJingLiActivity.this,"com.tencent.mobileqq")){
                            startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+413073223+"&version=1")));
                        }else {
                            Toast.makeText(KeHuJingLiActivity.this,"本机未安装QQ应用",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                //null没有任何逻辑 只是关闭Dialog
                aBuilder1.setNegativeButton("取消", null);
                //最后 不要show();
                aBuilder1.show();
                break;
        }
    }
    //判断是否安装QQ应用
    public boolean checkApkExist(Context context,String packageName){
        if (packageName == null || "".equals(packageName)){
            return false;
        }
        try {
            ApplicationInfo info = context.getPackageManager().
                    getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
